<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>cgo的使用 | Abel&#39;Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="golang可以通过cgo直接来调用c&#x2F;c++的函数代码。这里是参考《Go语言高级编程》书，做一些笔记。结构按照书的章节来。  1.快速入手 2.CGO基础 2.1.操作系统区分   3.类型转换 4.函数调用 6.实例 10.编译&#x2F;链接参数 参考  1.快速入手12345678&#x2F;&#x2F; hello.gopackage mainimport &quot;C&quot;func main() &amp;#123">
<meta property="og:type" content="article">
<meta property="og:title" content="cgo的使用">
<meta property="og:url" content="http://example.com/2021/07/28/2020/2020-12-24-cgo%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="Abel&#39;Blog">
<meta property="og:description" content="golang可以通过cgo直接来调用c&#x2F;c++的函数代码。这里是参考《Go语言高级编程》书，做一些笔记。结构按照书的章节来。  1.快速入手 2.CGO基础 2.1.操作系统区分   3.类型转换 4.函数调用 6.实例 10.编译&#x2F;链接参数 参考  1.快速入手12345678&#x2F;&#x2F; hello.gopackage mainimport &quot;C&quot;func main() &amp;#123">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-07-28T03:33:01.230Z">
<meta property="article:modified_time" content="2021-02-18T02:52:27.134Z">
<meta property="article:author" content="Abel Sean">
<meta property="article:tag" content="golang">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Abel&#39;Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Abel&#39;Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-2020/2020-12-24-cgo使用" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/07/28/2020/2020-12-24-cgo%E4%BD%BF%E7%94%A8/" class="article-date">
  <time datetime="2021-07-28T03:33:01.230Z" itemprop="datePublished">2021-07-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      cgo的使用
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>golang可以通过cgo直接来调用c/c++的函数代码。这里是参考《Go语言高级编程》书，做一些笔记。结构按照书的章节来。</p>
<ul>
<li><a href="#1%E5%BF%AB%E9%80%9F%E5%85%A5%E6%89%8B">1.快速入手</a></li>
<li><a href="#2cgo%E5%9F%BA%E7%A1%80">2.CGO基础</a><ul>
<li><a href="#21%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%8C%BA%E5%88%86">2.1.操作系统区分</a></li>
</ul>
</li>
<li><a href="#3%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2">3.类型转换</a></li>
<li><a href="#4%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8">4.函数调用</a></li>
<li><a href="#6%E5%AE%9E%E4%BE%8B">6.实例</a></li>
<li><a href="#10%E7%BC%96%E8%AF%91%E9%93%BE%E6%8E%A5%E5%8F%82%E6%95%B0">10.编译/链接参数</a></li>
<li><a href="#%E5%8F%82%E8%80%83">参考</a></li>
</ul>
<h1 id="1-快速入手"><a href="#1-快速入手" class="headerlink" title="1.快速入手"></a>1.快速入手</h1><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// hello.go</span></span><br><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;C&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="built_in">println</span>(<span class="string">&quot;hello cgo&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="2-CGO基础"><a href="#2-CGO基础" class="headerlink" title="2.CGO基础"></a>2.CGO基础</h1><p>需要安装C/C++构建工具链，在macOS和Linux下是要安装GCC，在windows下是需要安装MinGW工具。同时需要保证环境变量CGO_ENABLED被设置为1，这表示CGO是被启用的状态。</p>
<p>Windows里面使用的MinGW工具</p>
<figure class="highlight bat"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">mingw64\mingw64\bin&gt;.\g++.exe --version</span><br><span class="line">g++.exe (x86_64-win32-seh-rev0, Built by MinGW-W64 project) <span class="number">8</span>.<span class="number">1</span>.<span class="number">0</span></span><br><span class="line">Copyright (C) <span class="number">2018</span> Free Software Foundation, Inc.</span><br><span class="line">This is free software; see the source <span class="keyword">for</span> copying conditions.  There is NO</span><br><span class="line">warranty; <span class="keyword">not</span> even <span class="keyword">for</span> MERCHANTABILITY or FITNESS <span class="keyword">FOR</span> A PARTICULAR PURPOSE.</span><br><span class="line"></span><br><span class="line">// 在 strawberryperl 也有一个版本。</span><br><span class="line">PS D:\work\trunk\doc&gt; gcc --version</span><br><span class="line">gcc.exe (x86_64-posix-seh, Built by strawberryperl.com project) <span class="number">8</span>.<span class="number">3</span>.<span class="number">0</span></span><br><span class="line">Copyright (C) <span class="number">2018</span> Free Software Foundation, Inc.</span><br><span class="line">This is free software; see the source <span class="keyword">for</span> copying conditions.  There is NO</span><br><span class="line">warranty; <span class="keyword">not</span> even <span class="keyword">for</span> MERCHANTABILITY or FITNESS <span class="keyword">FOR</span> A PARTICULAR PURPOSE.</span><br><span class="line"></span><br><span class="line">It includes perl binaries, compiler (gcc) + related tools, all the external libraries (crypto, math, graphics, xml…), all the bundled database clients and all you expect from Strawberry Perl.</span><br></pre></td></tr></table></figure>

<p><a href="#%E5%8F%82%E8%80%83">gcc编译器对标准支持情况</a> gcc 8.x 版本的编译器已经能编译大部分c++17标准的东西了。</p>
<h2 id="2-1-操作系统区分"><a href="#2-1-操作系统区分" class="headerlink" title="2.1.操作系统区分"></a>2.1.操作系统区分</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#cgo windows CFLAGS: -DCGO_OS_WINDOWS=1</span></span><br><span class="line"><span class="comment">#cgo darwin CFLAGS: -DCGO_OS_DARWIN=1</span></span><br><span class="line"><span class="comment">#cgo linux CFLAGS: -DCGO_OS_LINUX=1</span></span><br></pre></td></tr></table></figure>

<h1 id="3-类型转换"><a href="#3-类型转换" class="headerlink" title="3.类型转换"></a>3.类型转换</h1><table>
<thead>
<tr>
<th align="left">C语言类型</th>
<th align="left">CGO类型</th>
<th align="left">Go语言类型</th>
</tr>
</thead>
<tbody><tr>
<td align="left">char</td>
<td align="left">C.char</td>
<td align="left">byte</td>
</tr>
<tr>
<td align="left">singed char</td>
<td align="left">C.schar</td>
<td align="left">int8</td>
</tr>
<tr>
<td align="left">unsigned char</td>
<td align="left">C.uchar</td>
<td align="left">uint8</td>
</tr>
<tr>
<td align="left">short</td>
<td align="left">C.short</td>
<td align="left">int16</td>
</tr>
<tr>
<td align="left">unsigned short</td>
<td align="left">C.ushort</td>
<td align="left">uint16</td>
</tr>
<tr>
<td align="left">int</td>
<td align="left">C.int</td>
<td align="left">int32</td>
</tr>
<tr>
<td align="left">unsigned int</td>
<td align="left">C.uint</td>
<td align="left">uint32</td>
</tr>
<tr>
<td align="left">long</td>
<td align="left">C.long</td>
<td align="left">int32</td>
</tr>
<tr>
<td align="left">unsigned long</td>
<td align="left">C.ulong</td>
<td align="left">uint32</td>
</tr>
<tr>
<td align="left">long long int</td>
<td align="left">C.longlong</td>
<td align="left">int64</td>
</tr>
<tr>
<td align="left">unsigned long long int</td>
<td align="left">C.ulonglong</td>
<td align="left">uint64</td>
</tr>
<tr>
<td align="left">float</td>
<td align="left">C.float</td>
<td align="left">float32</td>
</tr>
<tr>
<td align="left">double</td>
<td align="left">C.double</td>
<td align="left">float64</td>
</tr>
<tr>
<td align="left">size_t</td>
<td align="left">C.size_t</td>
<td align="left">uint</td>
</tr>
</tbody></table>
<h1 id="4-函数调用"><a href="#4-函数调用" class="headerlink" title="4.函数调用"></a>4.函数调用</h1><h1 id="6-实例"><a href="#6-实例" class="headerlink" title="6.实例"></a>6.实例</h1><h1 id="10-编译-链接参数"><a href="#10-编译-链接参数" class="headerlink" title="10.编译/链接参数"></a>10.编译/链接参数</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">因此CGO提供了CFLAGS/CPPFLAGS/CXXFLAGS三种参数，</span><br><span class="line">其中CFLAGS对应C语言编译参数(以.c后缀名)、</span><br><span class="line">CPPFLAGS对应C/C++ 代码编译参数(.c,.cc,.cpp,.cxx)、</span><br><span class="line">CXXFLAGS对应纯C++编译参数(.cc,.cpp,*.cxx)。</span><br></pre></td></tr></table></figure>

<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><ul>
<li>[1] <a target="_blank" rel="noopener" href="https://books.studygolang.com/advanced-go-programming-book/ch2-cgo/readme.html">Go语言高级编程</a></li>
<li>[2] <a target="_blank" rel="noopener" href="https://github.com/golang-china/gopl-zh">Go语言圣经</a></li>
<li>[3] <a target="_blank" rel="noopener" href="https://strawberryperl.com/">Strawberryperl官网</a></li>
<li>[4] <a target="_blank" rel="noopener" href="https://zh.cppreference.com/w/cpp/compiler_support">gcc编译器对标准支持情况</a></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/07/28/2020/2020-12-24-cgo%E4%BD%BF%E7%94%A8/" data-id="ckrn3an7a001920xwhw8ggwq2" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/golang/" rel="tag">golang</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/07/28/2020/2020-12-29-3d%E6%95%B0%E5%AD%A6/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          3d数学
        
      </div>
    </a>
  
  
    <a href="/2021/07/28/2020/2020-12-15-go%E8%AF%AD%E8%A8%80%E5%B9%B6%E5%8F%91%E4%B9%8B%E9%81%93%E7%AC%94%E8%AE%B0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">go语言并发之道笔记</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/3D/" rel="tag">3D</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/AI/" rel="tag">AI</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/" rel="tag">C++</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Game/" rel="tag">Game</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Go/" rel="tag">Go</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Math/" rel="tag">Math</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Server/" rel="tag">Server</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Unreal-Engine4/" rel="tag">Unreal Engine4</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/" rel="tag">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/golang/" rel="tag">golang</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/guitar/" rel="tag">guitar</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/node-js/" rel="tag">node.js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/" rel="tag">protobuf</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/recast/" rel="tag">recast</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/type/" rel="tag">type</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vscode/" rel="tag">vscode</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BF%90%E7%BB%B4/" rel="tag">运维</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/3D/" style="font-size: 10px;">3D</a> <a href="/tags/AI/" style="font-size: 11.67px;">AI</a> <a href="/tags/C/" style="font-size: 15px;">C++</a> <a href="/tags/Game/" style="font-size: 18.33px;">Game</a> <a href="/tags/Go/" style="font-size: 13.33px;">Go</a> <a href="/tags/Linux/" style="font-size: 13.33px;">Linux</a> <a href="/tags/Math/" style="font-size: 11.67px;">Math</a> <a href="/tags/Server/" style="font-size: 16.67px;">Server</a> <a href="/tags/Unreal-Engine4/" style="font-size: 10px;">Unreal Engine4</a> <a href="/tags/git/" style="font-size: 10px;">git</a> <a href="/tags/golang/" style="font-size: 20px;">golang</a> <a href="/tags/guitar/" style="font-size: 10px;">guitar</a> <a href="/tags/node-js/" style="font-size: 10px;">node.js</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/python/" style="font-size: 10px;">python</a> <a href="/tags/recast/" style="font-size: 11.67px;">recast</a> <a href="/tags/redis/" style="font-size: 10px;">redis</a> <a href="/tags/type/" style="font-size: 10px;">type</a> <a href="/tags/vscode/" style="font-size: 10px;">vscode</a> <a href="/tags/%E8%BF%90%E7%BB%B4/" style="font-size: 16.67px;">运维</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/28/2021/2021-07-19-etcd-learn/">go-etcd阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/07/28/2021/2021-06-22-go-ethereum-learn/">go-ethereum阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/07/28/2021/2021-06-21-server%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7/">server测试工具</a>
          </li>
        
          <li>
            <a href="/2021/07/28/2021/2021-06-09-pprof-doc/">go-pprof</a>
          </li>
        
          <li>
            <a href="/2021/07/28/2021/2021-04-19-c++-metaprogramming/">c++metaprogramming</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 Abel Sean<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>